package com.fox.stock.control;


import com.fox.stock.pojo.domain.PermissionDomain;
import com.fox.stock.pojo.domain.SysUserInfoDomain;
import com.fox.stock.pojo.entity.SysPermission;
import com.fox.stock.pojo.entity.SysRole;
import com.fox.stock.pojo.entity.SysUser;
import com.fox.stock.pojo.vo.resq.PageResult;
import com.fox.stock.service.impl.UserServiceImpl;
import com.fox.stock.pojo.vo.req.LoginReqVo;
import com.fox.stock.pojo.vo.req.UserSearchByConditionReqVo;
import com.fox.stock.pojo.vo.resq.LoginRespVo;
import com.fox.stock.pojo.vo.resq.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api("/api")
@RestController
@RequestMapping("/api")
public class UserControl {

    @Autowired
    private UserServiceImpl userService;

    // 根据用户名查询用户
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "username", value = "", required = true)
    })
    @ApiOperation(value = "根据用户名查询用户信息", notes = "根据用户名查询用户", httpMethod = "GET")
    @ApiImplicitParam(name = "username",value = "用户名字",required = true,dataType = "String",paramType = "path")
    @GetMapping("/{username}")
    @PreAuthorize("hasAuthority('sys:user:list')")
    public SysUser selectByUsername(@PathVariable("username")String username){
        return userService.selectByUsernameToSysUser(username);
    }


    // 登录的control方法
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "body", dataType = "LoginReqVo", name = "loginReqVo", value = "", required = true)
//    })
//    @ApiOperation(value = "用户登录接口", notes = "登录的control方法", httpMethod = "POST")
//    @PostMapping("/login")
//    public R<LoginRespVo> login(@RequestBody LoginReqVo loginReqVo){
//        return userService.login(loginReqVo);
//    }

    // 生成验证码并返回的方法
    @ApiOperation(value = "生成验证码的接口", notes = "生成验证码并返回的方法", httpMethod = "GET")
    @GetMapping("/captcha")
    public R<Map> getCaptchaCode(){
        return userService.getCaptchaCode();
    }

    /**
     * 多条件综合查询用户分页信息
     * @param userSearchByConditionReqVo 查询条件对象
     * @return 符合的用户信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "UserSearchByConditionReqVo", name = "userSearchByConditionReqVo", value = "查询条件对象", required = true)
    })
    @ApiOperation(value = "多条件综合查询用户分页信息", notes = "多条件综合查询用户分页信息", httpMethod = "POST")
    @PostMapping("/users")
    @PreAuthorize("hasAuthority('sys:user:list')")
    public R<PageResult> findUserInfoByCondition(@RequestBody UserSearchByConditionReqVo userSearchByConditionReqVo){
        return userService.findUserInfoByCondition(userSearchByConditionReqVo);
    }

    /**
     * 添加用户信息
     * @param sysUser 用户信息
     * @return 成功信息或者失败信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysUser", name = "sysUser", value = "用户信息", required = true)
    })
    @ApiOperation(value = "添加用户信息", notes = "添加用户信息", httpMethod = "POST")
    @PostMapping("/user")
    @PreAuthorize("hasAuthority('sys:user:add')")
    public R addUserInfo(@RequestBody SysUser sysUser){
        return userService.addUserInfo(sysUser);
    }


    /**
     * 获取用户具有的角色信息和所有角色信息
     * @param userId 用户id
     * @return 用户拥有的角色信息, 以及所有角色信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "userId", value = "用户id", required = true)
    })
    @ApiOperation(value = "获取用户具有的角色信息和所有角色信息", notes = "获取用户具有的角色信息和所有角色信息", httpMethod = "GET")
    @GetMapping("/user/roles/{userId}")
    public R<Map<String,Object>> getRolesInfo(@PathVariable String userId){
        return userService.getRolesInfo(userId);
    }

    /**
     * 更新用户角色数据
     * @param date 用户id和角色id集合
     * @return 操作信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Map<String, Object>", name = "date", value = "用户id和角色id集合", required = true)
    })
    @ApiOperation(value = "更新用户角色数据", notes = "更新用户角色数据", httpMethod = "PUT")
    @PutMapping("/user/roles")
    @PreAuthorize("hasAuthority('sys:user:role:update')")
    public R upDateUserRolesInfo(@RequestBody Map<String,Object> date){
        return userService.upDateUsereRolesInfo(date);
    }

    /**
     * 批量删除用户信息
     * @param list 要删除的用户id
     * @return 操作信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "List<Long>", name = "list", value = "要删除的用户id", required = true)
    })
    @ApiOperation(value = "批量删除用户信息", notes = "批量删除用户信息", httpMethod = "DELETE")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    @DeleteMapping("/user")
    public R deleteUserInfoByUserId(@RequestBody List<Long> list){
        return userService.deleteUserInfoByUserId(list);
    }

    /**
     * 根据用户id查询用户信息
     * @param id 用户id
     * @return 用户信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "id", value = "用户id", required = true)
    })
    @ApiOperation(value = "根据用户id查询用户信息", notes = "根据用户id查询用户信息", httpMethod = "GET")
    @GetMapping("/user/info/{userId}")
    @PreAuthorize("hasAuthority('sys:user:list')")
    public R<SysUser> findUserInfoById(@PathVariable("userId") String id){
        return userService.findUserInfoById(id);
    }

    /**
     * 根据id更新用户基本信息
     * @param sysUser 前端传过来的需要修改的基本信息
     * @return 操作信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysUserInfoDomain", name = "sysUser", value = "前端传过来的需要修改的基本信息", required = true)
    })
    @ApiOperation(value = "根据id更新用户基本信息", notes = "根据id更新用户基本信息", httpMethod = "PUT")
    @PutMapping("/user")
    @PreAuthorize("hasAuthority('sys:user:update')")
    public R upDateUserInfoById(@RequestBody SysUserInfoDomain sysUser){
        return userService.upDateUserInfoById(sysUser);
    }

    /**
     * 分页查询当前角色信息
     * @param map 装前端传来的pageNum 和 pageSize
     * @return 查询到的角色信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Map<String, Object>", name = "map", value = "装前端传来的pageNum 和 pageSize", required = true)
    })
    @ApiOperation(value = "分页查询当前角色信息", notes = "分页查询当前角色信息", httpMethod = "POST")
    @PostMapping("/roles")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public R<PageResult<SysRole>> selectRoleInfoByPage(@RequestBody Map<String,Object> map) {
        return userService.selectRoleInfoByPage(map);
    }

    /**
     * 树状结构回显权限集合
     * @return 所有的树状权限集合
     */
    @ApiOperation(value = "树状结构回显权限集合", notes = "树状结构回显权限集合", httpMethod = "GET")
    @GetMapping("/permissions/tree/all")
    public R<List<PermissionDomain>> getAllPermissionTree(){
        return userService.getAllPermissionTree();
    }

    /**
     * 添加角色和角色关联权限
     * @return 操作信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Map<String, Object>", name = "map", value = "", required = true)
    })
    @ApiOperation(value = "添加角色和角色关联权限", notes = "添加角色和角色关联权限", httpMethod = "POST")
    @PostMapping("/role")
    @PreAuthorize("hasAuthority('sys:role:add')")
    public R addRoleAndPermission(@RequestBody Map<String,Object> map) {
        return userService.addRoleAndPermission(map);
    }

    /**
     * 根据角色id返回权限集合
     * @param roleId 角色id
     * @return 权限id集合
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "roleId", value = "角色id", required = true)
    })
    @ApiOperation(value = "根据角色id返回权限集合", notes = "根据角色id返回权限集合", httpMethod = "GET")
    @GetMapping("/role/{roleId}")
    public R<List<String>> getPermissionByroleId(@PathVariable String roleId){
        return userService.getPermissionByroleId(roleId);
    }

    /**
     * 更新角色信息和角色相关的权限集合
     * @param map 要更改的角色信息和权限id
     * @return 操作信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Map<String, Object>", name = "map", value = "要更改的角色信息和权限id", required = true)
    })
    @ApiOperation(value = "更新角色信息和角色相关的权限集合", notes = "更新角色信息和角色相关的权限集合", httpMethod = "PUT")
    @PutMapping("/role")
    public R upDateRoleAndRolePermission(@RequestBody Map<String,Object> map) {
        return userService.upDateRoleAndPermission(map);
    }

    /**
     * 通过角色id删除角色和角色关联权限
     * @param roleId 角色id
     * @return 操作信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "roleId", value = "角色id", required = true)
    })
    @ApiOperation(value = "通过角色id删除角色和角色关联权限", notes = "通过角色id删除角色和角色关联权限", httpMethod = "DELETE")
    @DeleteMapping("/role/{roleId}")
    public R deleteRoleAndPermissionByRoleId(@PathVariable String roleId){
        return userService.deleteRoleAndPermissionByRoleId(roleId);
    }

    /**
     * 更新用户的状态信息
     * @param roleId 用户id
     * @param status 用户状态
     * @return 操作信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "roleId", value = "用户id", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "status", value = "用户状态", required = true)
    })
    @ApiOperation(value = "更新用户的状态信息", notes = "更新用户的状态信息", httpMethod = "POST")
    @PostMapping("/role/{roleId}/{status}")
    public R updateRoleStatus(@PathVariable Long roleId,@PathVariable Integer status){
        return userService.updateRoleStatus(roleId,status);
    }

    /**
     * 查询所有权限集合
     * @return 所有权限集合信息
     */
    @ApiOperation(value = "查询所有权限集合", notes = "查询所有权限集合", httpMethod = "GET")
    @GetMapping("/permissions")
    public R<List<SysPermission>> getAllPermissions(){
        return userService.getAllPermissionInfo();
    }

    /**
     * 添加权限时回显权限树,仅显示目录和caidan
     * @return 返回的权限树
     */
    @ApiOperation(value = "添加权限时回显权限树,仅显示目录和caidan", notes = "添加权限时回显权限树,仅显示目录和caidan", httpMethod = "GET")
    @GetMapping("/permissions/tree")
    public R<List<Map<String,Object>>> getAllPermissionTreeIgnore3(){
        return userService.getAllPermissionTreeIgnore3();
    }

    /**
     * 权限添加
     * @param sysPermission 权限类
     * @return 删除信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysPermission", name = "sysPermission", value = "权限类", required = true)
    })
    @ApiOperation(value = "权限添加", notes = "权限添加", httpMethod = "POST")
    @PostMapping("/permission")
    public R addPermission(@RequestBody SysPermission sysPermission){
        return userService.addPermission(sysPermission);
    }

    /**
     * 更新权限
     * @param sysPermission 需要更新的权限类
     * @return 操作信息
     */
    @PutMapping("permission")
    public R upDatePermission(@RequestBody SysPermission sysPermission){
        return userService.upDatePermission(sysPermission);
    }


    /**root
     *
     * 删除权限
     * @param permissionId 权限id
     * @return 删除信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "permissionId", value = "权限id", required = true)
    })
    @ApiOperation(value = "删除权限", notes = "删除权限", httpMethod = "DELETE")
    @DeleteMapping("/permission/{permissionId}")
    public R deletePermissionById(@PathVariable Long permissionId){
        return userService.deletePermissionById(permissionId);
    }

}
